package com.wenyu.wenyuyuedu.fragment;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.adapter.HomePageTabAdapter;
import com.wenyu.wenyuyuedu.fragment.homepagefragment.AllAttentionFragment;
import com.wenyu.wenyuyuedu.fragment.homepagefragment.AllFragment;
import com.wenyu.wenyuyuedu.fragment.homepagefragment.DeityAttentionFragment;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class HomePageFragment extends Fragment {


    private View homePageView;
    private TabLayout tab_homepage_title;    //定义TabLayout
    private ViewPager vp_homepage_pager;     //定义viewPager
    private AllFragment allFragment;         //全部
    private AllAttentionFragment allAttentionFragment;      //我的关注
    private DeityAttentionFragment deityAttentionFragment;      //神贴
    private FragmentPagerAdapter fAdapter;       //定义adapter
    private List<Fragment> list_fragment;        //定义要装fragment的集合
    private List<String> list_title;             //tab名称列表

    public HomePageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        homePageView = inflater.inflate(R.layout.fragment_home_page, container, false);
        StatusBarUtil.setStatusBar(getActivity(), R.color.white);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        initControls(homePageView);
        return homePageView;
    }

    /*
     * 初始化控件
     * */
    private void initControls(View view) {
        tab_homepage_title = (TabLayout) view.findViewById(R.id.tab_homepage_title);
        vp_homepage_pager = (ViewPager) view.findViewById(R.id.vp_homepager_pager);
        //初始化三个fragment
        allFragment = new AllFragment();
        allAttentionFragment = new AllAttentionFragment();
        deityAttentionFragment = new DeityAttentionFragment();
        //将fragment存放到集合当中
        list_fragment = new ArrayList<>();
        list_fragment.add(allFragment);
        list_fragment.add(allAttentionFragment);
        list_fragment.add(deityAttentionFragment);
        //定义存放名字的集合
        list_title = new ArrayList<>();
        list_title.add("全部");
        list_title.add("关注");
        list_title.add("神呐");
        //设置TabLayout的模式
        tab_homepage_title.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tab_homepage_title.addTab(tab_homepage_title.newTab().setText(list_title.get(0)));
        tab_homepage_title.addTab(tab_homepage_title.newTab().setText(list_title.get(1)));
        tab_homepage_title.addTab(tab_homepage_title.newTab().setText(list_title.get(2)));
        fAdapter = new HomePageTabAdapter(getActivity().getSupportFragmentManager(), list_fragment, list_title);
        //viewpager加载adapter
        vp_homepage_pager.setAdapter(fAdapter);
        //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
        //TabLayout加载viewpager
        tab_homepage_title.setupWithViewPager(vp_homepage_pager);
        //tab_FindFragment_title.set
    }
/*    由于最近项目需要,需要设置tabLayout下方下划线的长度.笔者上网找了半天,也没有找到方法.后来了解到在源码中对tabLayout的下划线进行了设置.并没有方法可以直接设置.
然后,笔者看到了某位大神的代码,利用反射修改下划线的宽度.先上代码:*/
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
}